using MapsterMapper;
using AiQiuQuan.Sport.Model.Models;
using AiQiuQuan.Sport.Core;
using AiQiuQuan.Sport.WebApi.Entities;
using AiQiuQuan.Sport.WebApi.Repositories;
using Microsoft.EntityFrameworkCore;
using AiQiuQuan.Sport.Model.Enums;

namespace AiQiuQuan.Sport.WebApi.Services.Impl
{
    /// <summary>
    /// 管理
    /// </summary>
    public class VenueInfoAccountService : IVenueInfoAccountService
    {
        private readonly IMapper _mapper;
        private readonly IUnitOfWorkManager _unitOfWorkManager;
        private readonly IVenueInfoAccountRepository _venueInfoAccountRepository;
        private readonly IVenueInfoAccountDetailRepository _venueInfoAccountDetailRepository;

        /// <summary>
        /// ctor
        /// </summary>
        public VenueInfoAccountService(
            IMapper mapper,
            IUnitOfWorkManager unitOfWorkManager,
            IVenueInfoAccountRepository venueInfoAccountRepository,
            IVenueInfoAccountDetailRepository venueInfoAccountDetailRepository)
        {
            _mapper = mapper;
            _unitOfWorkManager = unitOfWorkManager;
            _venueInfoAccountRepository = venueInfoAccountRepository;
            _venueInfoAccountDetailRepository = venueInfoAccountDetailRepository;
        }

        /// <summary>
        /// 增加场馆账户及明细记录
        /// </summary>
        public async Task<UnaryResult> AddDetailListAsync(List<VenueInfoAccountDetailAddDto> modelList)
        {
            var detailList = new List<VenueInfoAccountDetail>();
            foreach (var model in modelList)
            {
                var isExistAccount = true;
                var account = await _venueInfoAccountRepository.FirstOrDefaultAsync(m => m.VenueInfoID == model.VenueID);
                if (account == null)
                {
                    isExistAccount = false;
                    account = new VenueInfoAccount
                    {
                        ID = Guid.NewGuid(),
                        VenueInfoID = model.VenueID,
                        AccountTotal = 0,
                        CanWithdrawTotal = 0,
                        FrozenTotal = 0,
                        IsCanUse = true
                    };
                }

                var detail = new VenueInfoAccountDetail
                {
                    VenueInfoAccountID = account.ID,
                    VIAD_Type = model.OperateType,
                    VIAD_Num = model.Amount,
                    VIAD_Explain = model.Remark,
                    VIAD_Source = model.SourceType,
                    VIAD_BeforeNum = account.AccountTotal,
                };
                switch (model.OperateType)
                {
                    case VenueAccountOperateEnum.Add:
                        detail.VIAD_AfterNum = detail.VIAD_BeforeNum + detail.VIAD_Num;
                        account.AccountTotal += detail.VIAD_Num;
                        account.CanWithdrawTotal += detail.VIAD_Num;
                        break;
                    case VenueAccountOperateEnum.Reduce:
                        detail.VIAD_AfterNum = detail.VIAD_BeforeNum - detail.VIAD_Num;
                        account.AccountTotal -= detail.VIAD_Num;
                        if (model.SourceType != VenueAccountSourceEnum.Withdraw)
                        {
                            account.CanWithdrawTotal -= detail.VIAD_Num;
                        }
                        else
                        {
                            account.FrozenTotal -= detail.VIAD_Num;
                        }

                        break;
                    case VenueAccountOperateEnum.UnFrozen:
                        detail.VIAD_AfterNum = detail.VIAD_BeforeNum;
                        account.FrozenTotal -= detail.VIAD_Num;
                        account.CanWithdrawTotal += detail.VIAD_Num;
                        break;
                    case VenueAccountOperateEnum.Frozen:
                        break;
                }

                using (var uow = _unitOfWorkManager.Begin())
                {
                    if (isExistAccount)
                    {
                        await _venueInfoAccountRepository.UpdateAsync(account);
                    }
                    else
                    {
                        await _venueInfoAccountRepository.AddAsync(account);
                    }

                    await _venueInfoAccountDetailRepository.AddAsync(detail);

                    await uow.CompleteAsync();
                }
            }

            return UnaryResult.Succeed();
        }

        /// <summary>
        /// 新增
        /// </summary>
        public async Task<UnaryResult> AddAsync(VenueInfoAccountAddDto model)
        {
            var venueInfoAccount = _mapper.Map<VenueInfoAccount>(model);
            await _venueInfoAccountRepository.AddAsync(venueInfoAccount);
            return UnaryResult.Succeed();
        }

        /// <summary>
        /// 删除
        /// </summary>
        public async Task<UnaryResult> DeleteAsync(Guid id)
        {
            await _venueInfoAccountRepository.DeleteAsync(p => p.ID == id);
            return UnaryResult.Succeed();
        }
    }
}
